const axios = require('axios')
const { Message } = require('element-ui');
const router = require('../../router/index');


module.exports = {
    namespaced: true,
    state: {
        //正常营业的门店
        shopsData1: [],
        //新增待审核的门店
        shopsData2: [],
        //关闭待审核的门店
        shopsData3: [],
        //已关闭的门店
        shopsData4: [],

        // 弹窗打开关闭
        dialogFormVisible: false,
        //修改的门店信息
        updateData: {
            _id: "",
            name: "",
            address: "",
            //  state:"",
            storeId: "",

        },


        // 分页
        pageData1: {
            pageSize: 4,
            currentPage: 1,
        },
        pageData2: {
            pageSize: 4,
            currentPage: 1,
        },
        pageData3: {
            pageSize: 4,
            currentPage: 1,
        },
        pageData4: {
            pageSize: 4,
            currentPage: 1,
        },
    },
    getters: {},
    mutations: {
        getShopsData1(state, data) {
            state.shopsData1 = data
        },
        getShopsData2(state, data) {
            state.shopsData2 = data
        },
        getShopsData3(state, data) {
            state.shopsData3 = data
        },
        getShopsData4(state, data) {
            state.shopsData4 = data
        },
        // 获取需要修改的学生数据
        updateShops(state, row) {
            // console.log(row)
            state.updateData = { ...row };
            state.dialogFormVisible = true;
        },
        // 关闭修改
        close(state) {
            state.dialogFormVisible = false;
            Message({
                type: "info",
                message: "已取消"
            });
        },
    },
    actions: {
        //列表渲染
        async getShopsData1Async(context) {
            const { data } = await axios.get('/shops/getShops',
                {
                    params: {
                        pageSize: context.state.pageData1.pageSize,
                        nowPage: context.state.pageData1.currentPage,
                        state: "正常",
                    }
                })
            context.state.pageData1.shopsTotal = data.total
            console.log(data)
            context.commit('getShopsData1', data.rows)
        },
        //列表渲染
        async getShopsData2Async(context) {
            const { data } = await axios.get('/shops/getShops',
                {
                    params: {
                        pageSize: context.state.pageData2.pageSize,
                        nowPage: context.state.pageData2.currentPage,
                        state: "新增待审核",
                    }
                })
            context.state.pageData2.shopsTotal = data.total
            // console.log(data)
            context.commit('getShopsData2', data.rows)
        },
        //列表渲染
        async getShopsData3Async(context) {
            const { data } = await axios.get('/shops/getShops',
                {
                    params: {
                        pageSize: context.state.pageData2.pageSize,
                        nowPage: context.state.pageData2.currentPage,
                        state: "关闭待审核",
                    }
                })
            context.state.pageData3.shopsTotal = data.total
            // console.log(data)
            context.commit('getShopsData3', data.rows)
        },
        //列表渲染
        async getShopsData4Async(context) {
            const { data } = await axios.get('/shops/getShops',
                {
                    params: {
                        pageSize: context.state.pageData3.pageSize,
                        nowPage: context.state.pageData3.currentPage,
                        state: "已关闭",
                    }
                })
            context.state.pageData4.shopsTotal = data.total
            // console.log(data)
            context.commit('getShopsData4', data.rows)
        },


        // 确认修改门店
        async sure(context, _id) {
            if (
                context.state.updateData.name != "" &&
                context.state.updateData.age != ""
            ) {
                //   修改门店
                const { data } = await axios.post("/shops/updateShops", context.state.updateData)
                // console.log(data)
                if (data) {
                    context.state.dialogFormVisible = false;
                    Message({
                        type: "success",
                        message: "修改成功！"
                    });
                    context.dispatch('getShopsData1Async')
                    context.dispatch('getShopsData2Async')
                    context.dispatch('getShopsData3Async')
                    context.dispatch('getShopsData4Async')
                } else {
                    Message.error("修改失败！");
                }
            } else {
                Message.error("请填写完整信息！");
            }
        },

        // 新增门店
        async addShops(context, shop) {
            console.log(shop)
            if (shop.name != "" && shop.address != "") {
                const { data } = await axios.post('/shops/addShops', shop)
                console.log(data)
                if (data) {
                    Message({
                        type: "success",
                        message: "新增成功！"
                    });
                    context.dispatch('getShopsData1Async')
                    context.dispatch('getShopsData2Async')
                    context.dispatch('getShopsData3Async')
                    context.dispatch('getShopsData4Async')
                } else {
                    Message.error("新增失败！");
                }
            } else {
                Message.error("请填写完整信息！");
            }
        },
        //关闭门店
        async closeShop(context, shop) {
            console.log(shop)
            shop.state = "关闭待审核"
            const { data } = await axios.post('/shops/updateShops', shop)
            // console.log(data)
            if (data) {
                Message({
                    type: "success",
                    message: "关闭申请成功！等待审核"
                });
                context.dispatch('getShopsData1Async')
                context.dispatch('getShopsData2Async')
                context.dispatch('getShopsData3Async')
                context.dispatch('getShopsData4Async')
            } else {
                Message.error("关闭申请失败！");
            }
        },
        //取消关闭审核
        async cancelCloseShop(context, shop) {
            shop.state = "正常"
            const { data } = await axios.post('/shops/updateShops', shop)
            // console.log(data)
            if (data) {
                Message({
                    type: "success",
                    message: "已取消关闭审核！"
                });
                context.dispatch('getShopsData1Async')
                context.dispatch('getShopsData2Async')
                context.dispatch('getShopsData3Async')
                context.dispatch('getShopsData4Async')
            } else {
                Message.error("取消关闭审核失败！");
            }
        },
        //取消新增审核
        async cancelAddShop(context, shop) {
            shop.state = "已关闭"
            const { data } = await axios.post('/shops/updateShops', shop)
            // console.log(data)
            if (data) {
                Message({
                    type: "success",
                    message: "已取消新增审核！"
                });
                context.dispatch('getShopsData1Async')
                context.dispatch('getShopsData2Async')
                context.dispatch('getShopsData3Async')
                context.dispatch('getShopsData4Async')
            } else {
                Message.error("取消新增审核失败！");
            }
        },
        // 解封门店
        async againOpen(context,shop){
            shop.state = "新增待审核"
            const { data } = await axios.post('/shops/updateShops', shop)
            // console.log(data)
            if (data) {
                Message({
                    type: "success",
                    message: "解封成功！等待管理员审核"
                });
                context.dispatch('getShopsData1Async')
                context.dispatch('getShopsData2Async')
                context.dispatch('getShopsData3Async')
                context.dispatch('getShopsData4Async')
            } else {
                Message.error("解封失败！");
            }
        },





        //改变当前pageSize
        handleSizeChange1(context, val) {
            context.state.pageData1.pageSize = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },
        // 改变当前currentPage
        handleCurrentChange1(context, val) {
            context.state.pageData1.currentPage = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },
        handleSizeChange2(context, val) {
            context.state.pageData2.pageSize = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },
        // 改变当前currentPage
        handleCurrentChange2(context, val) {
            context.state.pageData2.currentPage = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },
        handleSizeChange3(context, val) {
            context.state.pageData3.pageSize = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },
        // 改变当前currentPage
        handleCurrentChange3(context, val) {
            context.state.pageData3.currentPage = val
            context.dispatch('getShopsData1Async')
            context.dispatch('getShopsData2Async')
            context.dispatch('getShopsData3Async')
            context.dispatch('getShopsData4Async')
        },

    }
}